package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.SupervisionTaskDao;
import com.wlhse.entity.SupervisionTaskPojo;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.SupervisionTaskService;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import com.wlhse.dto.SupervisionTaskDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SupervisionTaskServiceImpl implements SupervisionTaskService {

    @Value("${RESOURCES_URL}")
    private String url;


    @Resource
    private SupervisionTaskDao supervisionTaskDao;

    @Override
    public String queryTaskSituation(SupervisionTaskDto supervisionTaskDto) {

        List<SupervisionTaskPojo> list;
        int total = supervisionTaskDao.getTaskcount(supervisionTaskDto);
        PageHelper.startPage(supervisionTaskDto.getPageIdx(), supervisionTaskDto.getPageSize());
        if (supervisionTaskDto.getStartDate()==null&&supervisionTaskDto.getEndDate()==null
                &&supervisionTaskDto.getStatus()==null&&supervisionTaskDto.getRound()==null
                &&supervisionTaskDto.getExecStaffID()==null)
        {
             list = supervisionTaskDao.queryAll(supervisionTaskDto);
        }
        else
        {
            list = supervisionTaskDao.queryTaskSituation(supervisionTaskDto);
        }
        return NR.r(list, total, supervisionTaskDto.getPageIdx());
    }

    @Override
    public String queryStaffTaskSituation(SupervisionTaskDto supervisionTaskDto) {
        int total = supervisionTaskDao.getstaffTaskcount(supervisionTaskDto);
        PageHelper.startPage(supervisionTaskDto.getPageIdx(), supervisionTaskDto.getPageSize());
        List<SupervisionTaskPojo> list = supervisionTaskDao.queryStaffTaskSituation(supervisionTaskDto);
        return NR.r(list, total, supervisionTaskDto.getPageIdx());
    }

    @Override
    public String queryexecStaffTaskSituation(SupervisionTaskDto supervisionTaskDto) {
        int total = supervisionTaskDao.getexecstaffTaskcount(supervisionTaskDto);
        PageHelper.startPage(supervisionTaskDto.getPageIdx(), supervisionTaskDto.getPageSize());
        List<SupervisionTaskPojo> list = supervisionTaskDao.queryexecStaffTaskSituation(supervisionTaskDto);
        return NR.r(list, total, supervisionTaskDto.getPageIdx());
    }

    @Override
    public String updateTaskStatus(SupervisionTaskDto supervisionTaskDto) {
        if (supervisionTaskDao.updateTaskStatus(supervisionTaskDto) <= 0)
        {
            throw new WLHSException("更新失败");
        }
        return NR.r();
    }

    @Override
    @Transactional
    public String deleteTask(String code) {
        List<String> status=supervisionTaskDao.querryTaskStatus(code);
        if("未接收".equals(status.get(0))){
            supervisionTaskDao.deleteTaskPersonByCode(code);
            supervisionTaskDao.deleteTaskdetailByCode(code);
            supervisionTaskDao.deleteTaskByCode(code);
            return NR.r();
        }else{
            return NR.r(CodeDict.CODE_MESSAGE,-1,CodeDict.DELETE_FAILE,null,null,0,0);
        }
    }
}
